<?php
//session_start();
require_once( 'service_db.php' );
require_once( 'student_db.php' );
 

$user_id = $_SESSION['userid'];
$m_db = new M_Service();
$Year = $m_db->getCurrentYear();
$Term = $m_db->getCurrentTerm();
if (isset($_POST['m'])  &&  $_POST['m']!=""){

	if ($_POST['m']=='c_detail'){
		$id=$_POST['id'];
		$result = $m_db->getCourseById($id);
		
		if($m_db->getRowCount() > 0 ){
			echo json_encode( array('v'=>1,'data'=>$result));
		}else{
			echo json_encode( array('v'=>0,'data'=>''));
		}
	}else if ($_POST['m']=='b_detail'){
		$id=$_POST['id'];
		$result = $m_db->getbusById($id);
		
		if($m_db->getRowCount() > 0 ){
			echo json_encode( array('v'=>1,'data'=>$result));
		}else{
			echo json_encode( array('v'=>0,'data'=>''));
		}

    }else  if ($_POST['m']== 'load'){
	  
	  $classid = $_POST['class'];
	  $room = $_POST['room'];
	  $year = $_POST['year'];
	  $term = $_POST['term'];
	  $name = $_POST['name'];
	  $code = $_POST['code'];
	  
      $result=$m_db->loadData($year,$term,$classid,$room,$code,$name);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           
            while($row = $result->FetchRow()){
			    
               $data[] = array(0,
			   $row['student_id']
			   ,$year.'/'.$term,
			   $row['code']
			   , $row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname']  
			   ,$row['thai_nickname']
			   ,$row['class_name'].' / '.$row['room']
			   ,$row['create_date']
			   ,$row['course']
			   ,$row['bus']	) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
     
    }else if ($_POST['m']=='room'){ 
		$id=  $_POST['cid'];
		echo json_encode( $m_db->load_room($id));
	}else if ($_POST['m']=='getRoom_amt'){
		$id=  $_POST['cid'];
		$result = $m_db->load_current_amt_room($id);
		$html="";
		while($row = $result->FetchRow()){
			$html.= "<span class='help-inline' style='font-weight:bold;'>ห้อง  ".$row['room']."&nbsp;:&nbsp;&nbsp;</span>";
			$color="";
			if ($row['cnt']<$row['amount']){
				if ($row['cnt'] > ($row['amount'] - 2)){
					$color = 'label-important'; //Nearly full
				}else{
					$color = 'label-success';
				}
			}else{
				$color = '';
			}
			$html.="<span class='label ".$color."' >".$row['cnt']."/".$row['amount']."</span> ";
		}
		 
		echo  $html;
	 
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		$list = $_POST['list'];
		foreach($list as $val){
			$ar['student_id'] = $val['sid'];
			$ar['class_id']=$val['cid'];
			$ar['room']=$val['r'];
			$ar['year']=$Year + 1;//  $Year;
			
			$m_db->student_AdjustLevel($ar);
		}
		echo 1;
    } else if ($_POST['m']=='update'){
		$id= $_POST['id'];
		$course = $_POST['course'];
		$bus = $_POST['bus'];
		
		 
		$m_db->AddCourse2($id,$course,$user_id);

		$m_db->AddBus2($id,$bus,$user_id);
		 
		
    } else if ($_POST['m']=='c_update'){
		$id= $_POST['id'];
		$type = $_POST['type'];
		//$bus = $_POST['bus'];
		if ($type=="c"){
			$course = $_POST['list'];	
			$teacher = $_POST['teacher_list'];
			if ($course!=''){
				$m_db->AddCourse($id,$course,$user_id);
				
				if ($teacher!=''){
					foreach($teacher as $data){
						$m_db->updateCourseTeacher($id, $data['cid'],$data['tid'],$Year, $Term);
					}
				}
				
			}else{
				echo $m_db->AbortCourse($id,$Year, $Term);
			}
		}else{
			$bus = $_POST['list'];
			if ($bus !='0'){
				echo $m_db->AddBus($id,$bus,$user_id);
			}else{
				echo $m_db->AbortRegisterBus($id,$Year, $Term);
			}
		}
	}
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		$m_student = new M_Student();
		if ($_GET['m'] == 'class'){
			echo json_encode( $m_student->getClassLevel());
		}else if ($_GET['m'] =='year'){
		  echo json_encode($m_student->getYear_Term());
		}else if ($_GET['m'] =='curryear'){
			echo $m_student->getCurrentYear();	
		}else if ($_GET['m'] =='term'){
		  echo json_encode($m_db->getTerm());
		}else if ($_GET['m']=='room'){
			echo  json_encode( $m_student->load_all_room($_GET['i']));
		}else if ($_GET['m'] =='currterm'){
			echo $m_db->getCurrentTerm();
		}
		
	} 
   
}

function DATEDIFF($date1, $date2){
	
}
 
?>